﻿// Modificado por Sergio. 02/06/10. Cambiadas las cosas relativas a la vida para que lo maneje la clase Life
// Modificado por Sergio. 15/06/10. Clase simplificada con el agregado de Transform (no estan mas pos,rot y demas)

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Engine;
using Engine.Pathfinding;

#endregion

namespace Game
{
    public class Enemy
    {
        Transform mTransform = new Transform();

        Vector3 playerPosition;
        Queue<Vector3> _path;

        Life mLife = new Life(40);
        int mSpeed;
        int mStrength;
        float elapsed;
        public bool isLeader;
        public Model mModel;
        public string AISwitch = "";
        Ray ray;
        
        public Enemy()
        {
            playerPosition = Vector3.Zero;
            _path = new Queue<Vector3>();
            isLeader = false;
            AISwitch = "ZombieFollow";
            ray = new Ray();
        }

        public Queue<Vector3> Path
        {
            get { return this._path; }
            set { this._path = value; }
        }

        public Life Life
        {
            get { return mLife; }
            set { mLife = value; }
        }


        public void AI(GameTime _time)
        {
            elapsed += (float)_time.ElapsedGameTime.Seconds;

            switch(AISwitch)
            {
                case "EnterHouse":
                    if (_path != null && _path.Count != 0)
                    {
                        mTransform.LookAt(_path.Peek());
                        mTransform.Position += mTransform.Forward * 6;

                        if (Vector3.Distance(new Vector3(_path.Peek().X, _path.Peek().Y, _path.Peek().Z), mTransform.Position) < 100)
                        {
                            _path.Dequeue();
                        }
                    } 
                break;
                case "ZombieFollow":
                    break;
                case "ZombieAttack":
                    mTransform.LookAt(playerPosition);
                    mTransform.Position += mTransform.Forward * 5;
                    break;
                default:
                    break;
            }            
        }

        public void Update(GameTime gameTime,Vector3 playerPos)
        {
            mTransform.LastPosition = mTransform.Position;
            playerPosition = playerPos;
            AI(gameTime);

            ray.Direction = mTransform.Forward;
            ray.Position = mTransform.Position;

            if (Vector3.Distance(mTransform.Position, playerPosition) < 2000)
            {
                AISwitch = "ZombieAttack";
            }
            else
            {
                AISwitch = "EnterHouse";
            }


        }
        public void Draw(SpriteBatch spriteBatch, Matrix view, Matrix projection)
        {
            if(this.Life.IsAlive)
                ModelDrawer.Draw("Zombie1", mTransform.WorldMatrix, view, projection);
        }
        public void TakeDamage(int damage)
        {
            mLife.HealthPoints -= damage;
        }
        public Transform Transform
        {
            get { return mTransform; }
        }
        public Vector3 Position
        {
            get { return this.mTransform.Position; }
            set { this.mTransform.Position = value; }
        }
    }
}
